const API = require('../../../../utils/api')
const app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
      userInfo:{},
      formShow:true,
      submitStatus:true,
      index: 0,
      array:['请选择您的职位','管理','企业主','员工'],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      if( options && options.inviterUserId ){
        let userInfo = {
            //昵称
            nickName: options.nickName,
            //公司名
            companyName: options.companyName,
            //公司id
            companyId: options.companyId,
            //邀请人id
            inviterUserId: options.inviterUserId,
            //头像
            headimgUrl: options.headimgUrl
        }
        this.setData({
            userInfo
        })
      }
      app.login(userData)
      function userData(data){
        this.setData({
          userInfo:data,
        })
      }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    //事件 - 显示资料填写
    showForm:function(){
      this.setData({
        formShow:false
      })
    },
  
    //事件 - 隐藏form资料填写
    hiddenForm:function(){
      this.setData({
        formShow:true
      })
    },

    //路由 = 赚佣金
    routComission:function(){
      wx.navigateTo({
        url: "/pages/mine/company-manage/company-manage",
      })
    },

    //事件 - 职位选择
    pickChange:function(e){
      this.setData({
        index:e.detail.value
      })
      console.log(e.detail.value);
    },

     //事件 - 表单提交
    formSubmit:function(e){
      // console.log(e.detail.value)
      let form = {}
      form.wxuserId = app.globalData.userInfo.id
      form.employeeName = e.detail.value.employeeName
      form.companyId = this.data.userInfo.companyId
      let index = this.data.index
      form.employeeIdentity = this.data.array[index]
      console.log(form)
      //表单验证
      if( !form.employeeName ){
          wx.showToast({
            title: '请填写您的姓名',
            icon:'none'
          })
          return
      }
      if( index == 0 ){
          wx.showToast({
            title: '请选择您的职位',
            icon:'none'
          })
          return
      }
      API.create_employee_info(form).then( res => {
        if( res.code == 200 ){
          this.setData({
            submitStatus:false
          })
        }else{
          wx.showToast({
            title: '提交失败，请稍后重试',
              icon:'none'
            })
        }
      })
    },
})